package xyz.eden.cloud.common.core.exception;

import java.util.Objects;

public interface AbstractException {

    /**
     * 获取数据
     *
     * @return
     */
    abstract ExceptionData getExceptionData();

    /**
     * 获取数据
     *
     * @param exceptionData
     * @param clazz
     * @param <T>
     * @return
     */
    default <T> T getData(ExceptionData exceptionData, Class<T> clazz) {
        if (Objects.isNull(exceptionData)) {
            return null;
        }

        if (Objects.isNull(clazz)) {
            return null;
        }

        try {
            Object data = exceptionData.getData();
            if (Objects.isNull(data)) {
                return null;
            }
            return clazz.cast(data);
        } catch (Exception e) {
            throw new UnsupportedOperationException(e);
        }
    }
}
